cmake_minimum_required(VERSION 3.5)

# ##############################################################################
# CMake Project ##
# ##############################################################################

# include useful macros
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/util.cmake)

# read metadata from package.xml and setup project
xict_cc_metadata()

# set project name and version
project(
  ${extracted_name}
  VERSION ${extracted_version}
  LANGUAGES C CXX)

configure_file(${PROJECT_SOURCE_DIR}/cmake/config.h.in
  ${PROJECT_SOURCE_DIR}/include/xict_common/config/config.h)
configure_file(${PROJECT_SOURCE_DIR}/cmake/version.h.in
  ${PROJECT_SOURCE_DIR}/include/xict_common/config/version.h @ONLY)

# =========#
# Settings #
# =========#

# introduce default building settings
xict_cc_settings()

set(EXTRA_DEPS "")
set(EXTRA_INCLUDES "")

option(ENABLE_YALK "Whether to enable YALK license management." OFF)

if(ENABLE_YALK)
  add_definitions(-DENABLE_YALK)
  include(${PROJECT_SOURCE_DIR}/../../cmake/yalk.cmake)
  list(APPEND EXTRA_DEPS ${YALK_LIB})
  list(APPEND EXTRA_INCLUDES ${YALK_INCLUDE_DIR})
endif()

option(SELFMADE_HANDLE "Whether to enable selfmade handle data publisher." OFF)

if(SELFMADE_HANDLE)
  add_definitions(-DSELFMADE_HANDLE)
endif()

option(GRADER "Whether to enable grader data publisher." OFF)

if(GRADER)
  add_definitions(-DGRADER)
endif()

option(LOADER "Whether to enable loader data publisher." OFF)

if(LOADER)
  add_definitions(-DLOADER)
endif()

option(BACKHOE_LOADER "Whether to enable backhoe loader data publisher." OFF)

if(BACKHOE_LOADER)
  add_definitions(-DBACKHOE_LOADER)
endif()

option(SCRAPER "Whether to enable scraper data publisher." OFF)

if(SCRAPER)
  add_definitions(-DSCRAPER)
endif()

# =============#
# Dependencies #
# =============#

# ----------------------#
# Third-party libraries #
# ----------------------#
find_package(Threads REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(OpenCV REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.0)
pkg_check_modules(GST_APP REQUIRED gstreamer-app-1.0)
pkg_check_modules(gstreamer-video REQUIRED IMPORTED_TARGET gstreamer-video-1.0)
find_package(fmt REQUIRED)
find_package(absl REQUIRED)
find_package(Catch2 REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(eCAL REQUIRED)
find_package(Protobuf REQUIRED)
find_package(Boost REQUIRED COMPONENTS system filesystem)
find_package(CUDA REQUIRED)
find_package(yaml-cpp REQUIRED)
find_package(Qt5 REQUIRED COMPONENTS Quick)

find_package(xict_proto REQUIRED)
# find_package(xict_ui REQUIRED)
find_package(xict_common REQUIRED)

set(CMAKE_CUDA_COMPILER /usr/local/cuda/bin/nvcc)
set(CMAKE_CUDA_ARCHITECTURES 61;75;86;89)
enable_language(CUDA)

include_directories(${CUDA_INCLUDE_DIRS})
link_directories(${CUDA_LIBRARIES})

# include_directories(/home/ps/tools/TensorRT-8.5.3.1/include)
# link_directories(/home/ps/tools/TensorRT-8.5.3.1/lib/)

# ##############################################################################
# Build ##
# ##############################################################################

# ==============#
# Build targets #
# ==============#
# 包含处理generate_export_header的模块
include(GenerateExportHeader)

# ---------------#
# Library target #
# ---------------#
include_directories(${PROJECT_SOURCE_DIR}/include)

file(GLOB_RECURSE PLUGIN_SRCS
  ${PROJECT_SOURCE_DIR}/src/xict_cpp/perception/plugin/*.cu)

# Find all source files
file(GLOB_RECURSE src_files ${PROJECT_SOURCE_DIR}/src/*/*.cc
  ${PROJECT_SOURCE_DIR}/src/*/*.cu)

# common library
# xict_cc_library(
# NAME
# myplugins
# SRCS
# ${PLUGIN_SRCS}
# INCLUDES
# ${EXTRA_INCLUDES}
# DEPS
# nvinfer
# cudart
# ${EXTRA_DEPS})
xict_cc_library(
  NAME
  ${PROJECT_NAME}
  SRCS
  ${src_files}
  INCLUDES
  ${OpenCV_INCLUDE_DIRS}
  ${GST_INCLUDE_DIRS}
  ${OpenSSL_INCLUDE_DIR}
  ${EXTRA_INCLUDES}
  DEPS
  dl
  Threads::Threads
  Eigen3::Eigen
  ${OpenCV_LIBS}
  ${GST_LIBRARIES}
  fmt::fmt
  absl::flags
  eCAL::core
  protobuf::libprotobuf
  nvinfer
  cudart
  absl::flags_parse
  xict_proto::xict_proto
  xict_common::xict_common
  # xict_ui::xict_ui
  Qt5::Quick
  ${OPENSSL_LIBRARIES}
  ${YAML_CPP_LIBRARIES}
  ${Boost_LIBRARIES}
  ${Boost_FILESYSTEM_LIBRARY}
  ${EXTRA_DEPS})

# copy the directory assets to ${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}
install(DIRECTORY ${PROJECT_SOURCE_DIR}/../../assets
  DESTINATION ${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME})

# ------------------#
# Executable targets #
# ------------------#
xict_cc_apps()

# -------------#
# Test targets #
# -------------#
include(CTest)
enable_testing()
xict_cc_tests("xict_cpp")
xict_cc_tests("xict_driver")
